package com.higgins.client.projectmanagement;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TreeItem;

public class ProjectListBox extends HorizontalPanel	{
	private ListBox listBox = new ListBox();
	private Button backButton = new Button("Back");
	private final ProjectServiceAsync projectService = GWT
						.create(ProjectService.class);
	private ArrayList<TreeListener> listBoxListeners = new ArrayList<TreeListener>();
	private Long parentId = null;
	private String level = null;
	
	public ProjectListBox(String level)	{
		this.level = level;
		initialise();
		attachWidgets();
	}
	private void initialise()	{
		listBox.addChangeHandler(new ChangeHandler()  {
			@Override
			public void onChange(ChangeEvent event) {
				int index = listBox.getSelectedIndex();
				String value = listBox.getValue(index);
				parentId = Long.parseLong(value);
				
				loadItems(parentId);
			}
			
		});
		backButton.addClickHandler(new ClickHandler()  {
			@Override
			public void onClick(ClickEvent event) {
				loadPreviousItems(parentId);
			}  });
		loadParentItems();
	}
	private void attachWidgets()	{
		this.add(listBox);
		this.add(backButton);
	}
	private void loadParentItems()	{
		projectService.retrieveProjectItems(level, null, new AsyncCallback<List<ProjectItemDTO>>()	{
			@Override
			public void onFailure(Throwable caught) {
			}
			@Override
			public void onSuccess(List<ProjectItemDTO> result) {
				loadListBox(result);
				if (listBoxListeners.size() > 0)	{
					updateRefreshables(result);
				} 
			}
		});		
	}
	private void loadItems(Long parentId)	{
		projectService.retrieveProjectItems(level, parentId, new AsyncCallback<List<ProjectItemDTO>>()	{
			@Override
			public void onFailure(Throwable caught) {
			}
			@Override
			public void onSuccess(List<ProjectItemDTO> result) {
				loadListBox(result);
				if (listBoxListeners.size() > 0)	{
					updateRefreshables(result);
				}
			}
		});		
	}
	private void loadPreviousItems(Long parentId)	{
		projectService.retrievePreviousProjectItems(parentId, new AsyncCallback<List<ProjectItemDTO>>()	{
			@Override
			public void onFailure(Throwable caught) {
			}
			@Override
			public void onSuccess(List<ProjectItemDTO> result) {
				loadListBox(result);
				if (listBoxListeners.size() > 0)	{
					updateRefreshables(result);
				}
			}
		});		
	}	
	private void loadListBox(List<ProjectItemDTO> result)	{
		listBox.clear();
		int index = 0;
		for (ProjectItemDTO dto:result)	{
			listBox.insertItem(dto.getTask(), dto.getId().toString(), index);
			index++;
		}
		listBox.setSelectedIndex(-1);
	}
	public void addListListener(TreeListener listBoxListener) {
		listBoxListeners.add(listBoxListener);
	}
	private void updateRefreshables(List<ProjectItemDTO> results)	{
		if (results.size()>0)	{
			ProjectItemDTO item = results.get(0);
			if (item.getParentId()!=null)	{
				parentId = item.getParentId();
			}
		}
		for (TreeListener listener:listBoxListeners)	{
			listener.update(results);
			listener.setParentId(parentId);
		}
	}
}
